package org.jeik.search;

import java.util.ArrayList;
import java.util.List;

import org.jeik.beackBeans.contents.PageContentsBean;
import org.jeik.beackBeans.search.SearchResultLink;
import org.jeik.data.models.Page;
import org.jeik.data.models.Service;
import org.jeik.utils.WebUtils;

/**
 * 
 * @author AAsha
 *
 */
public class SearchHandler {
	
	/**
	 * 
	 */
	public SearchHandler(){
		
	}
	
	/**
	 * 
	 * @param keyword
	 * @return
	 */
	public List<SearchResultLink> findPages(String keyword){
		
		SearchResultLink srl = null;
		List<SearchResultLink> list = new ArrayList<SearchResultLink>();
		
		PageContentsBean pageContentsBean = (PageContentsBean) WebUtils.resolveExpression(WebUtils.PAGE_CONTENTS_BEAN, PageContentsBean.class);
		
		for(Page page:pageContentsBean.getContents().values()){
			if(page.getBody().contains(keyword)){
				srl = new SearchResultLink(page.getPageNav(), page.getTitle());
				srl.setType("1");
				list.add(srl);
			}
		}
		
		return list;
	}
	
	/**
	 * 
	 * @param keyword
	 * @return
	 */
	public List<SearchResultLink> findServices(String keyword){
		
		SearchResultLink srl = null;
		List<SearchResultLink> list = new ArrayList<SearchResultLink>();
		
		PageContentsBean pageContentsBean = (PageContentsBean) WebUtils.resolveExpression(WebUtils.PAGE_CONTENTS_BEAN, PageContentsBean.class);
		
		for(Service service:pageContentsBean.getServices()){
			if(service.getBody().contains(keyword)){
				srl = new SearchResultLink(service.getId(), service.getTitle());
				srl.setType("2");
				list.add(srl);
			}
		}
		
		return list;
	}
	
}
